﻿using System;
using System.IO;
using Microsoft.Win32;
using System.Collections.Generic;

namespace BGShell.PowerShell.Runspaces
{
    internal sealed class UserProfiles : IEnumerable<FileInfo>
    {
        private readonly string _shellId;

        private string _allUsersDir;
        private string _userDir;

        public UserProfiles(string shellId)
        {
            _shellId = shellId;
        }

        public FileInfo AllUsersProfile
        {
            get { return GetProfile(AllUsersDirectory, null); } 
        }

        public FileInfo AllUsersHostProfile
        {
            get { return GetProfile(AllUsersDirectory, _shellId); }
        }

        public FileInfo UserProfile
        {
            get { return GetProfile(CurrentUserDirectory, null); }
        }

        public FileInfo UserHostProfile
        {
            get { return GetProfile(CurrentUserDirectory, _shellId); }
        }

        public IEnumerator<FileInfo> GetEnumerator()
        {
            return WhereExists(new FileInfo[]
            {
                AllUsersProfile,
                AllUsersHostProfile,
                UserProfile,
                UserHostProfile,
            });
        }

        private IEnumerator<FileInfo> WhereExists(IEnumerable<FileInfo> files)
        {
            foreach (var fi in files)
            {
                if ((fi != null) && (fi.Exists))
                {
                    yield return fi;
                }
            }
        }

        private string CurrentUserDirectory
        {
            get
            {
                if (_userDir == null)
                {
                    _userDir = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                        "WindowsPowerShell");
                }

                return _userDir;
            }
        }

        private string AllUsersDirectory
        {
            get
            {
                if (_allUsersDir == null)
                {
                    using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"))
                    {
                        _allUsersDir = (string)key.GetValue("ApplicationBase");
                    }
                }

                return _allUsersDir;
            }
        }

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        private static FileInfo GetProfile(string directory, string prefix)
        {
            var fileName = "profile.ps1";

            if (!string.IsNullOrEmpty(prefix))
            {
                fileName = prefix + '_' + fileName;
            }

            return new FileInfo(Path.Combine(directory, fileName));
        }
    }
}